package com.donleo.stmg.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author liangd
 * date 2021-01-03 19:26
 * code rabbitMq配置
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Queue empQueue() {
        return new Queue("EmpQueue", true);
    }

    @Bean
    public Queue leaderQueue() {
        return new Queue("LeaderQueue", true);
    }

    @Bean
    DirectExchange userDirectExchange() {
        return new DirectExchange("userDirectExchange", true, false);
    }

    @Bean
    FanoutExchange userFanoutExchange() {
        return new FanoutExchange("userFanoutExchange", true, false);
    }

    @Bean
    TopicExchange userTopicExchange() {
        return new TopicExchange("userTopicExchange", true, false);
    }

    @Bean
    Binding empQueueToUserDirect() {
        return BindingBuilder.bind(empQueue()).to(userDirectExchange()).with("emp.queue");
    }

    @Bean
    Binding leaderQueueToUserDirect() {
        return BindingBuilder.bind(empQueue()).to(userDirectExchange()).with("leader.queue");
    }

    @Bean
    Binding empQueueToUserFanout() {
        return BindingBuilder.bind(empQueue()).to(userFanoutExchange());
    }

    @Bean
    Binding leaderQueueToUserFanout() {
        return BindingBuilder.bind(leaderQueue()).to(userFanoutExchange());
    }

    @Bean
    Binding empQueueToUserTopic() {
        return BindingBuilder.bind(empQueue()).to(userTopicExchange()).with("emp.#");
    }

    @Bean
    Binding leaderQueueToUserTopic() {
        return BindingBuilder.bind(leaderQueue()).to(userTopicExchange()).with("leader.#");
    }

    /**
     * 消息序列化
     *
     * @return
     */
    @Bean
    MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

}
